package com.test.reggieadmin.model.employee;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.test.reggieadmin.R;
import com.test.reggieadmin.base.BaseActivity;
import com.test.reggieadmin.bean.BaseBean;
import com.test.reggieadmin.bean.EmployeeBean;
import com.test.reggieadmin.bean.EmployeeDetailsBean;
import com.test.reggieadmin.bean.State;
import com.test.reggieadmin.databinding.ActivityAddEmployeeBinding;
import com.test.reggieadmin.http.API;
import com.yechaoa.yutils.GsonUtil;
import com.yechaoa.yutils.ToastUtil;
import com.yechaoa.yutils.YUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 添加员工
 */
public class AddEmployeeActivity extends BaseActivity<ActivityAddEmployeeBinding, AddEmployeePrensenter>
        implements AddEmployeeView {

    private EmployeeBean employeeBean;
    private String sex = "1";
    private String id;

    @Override
    protected AddEmployeePrensenter createPresenter() {
        return new AddEmployeePrensenter(this);
    }

    @Override
    protected void initView() {
        viewBinding.icHead.tvTitle.setText("添加员工");
        employeeBean = GsonUtil.GsonToBean(String.valueOf(API.kv.decodeString(State.employee)), EmployeeBean.class);
        id = getIntent().getStringExtra("id");
        if (id != null && !"".equals(id)) {
            viewBinding.icHead.tvTitle.setText("修改");
            viewBinding.etUsername.setEnabled(false);//禁止修改用户名
            YUtils.showLoading("加载中...");
            presenter.employeeDetails(id);
        }
    }

    @Override
    protected void initData() {
        viewBinding.icHead.llBack.setOnClickListener(v -> finish());
        viewBinding.rg.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb1) {
                sex = "1";
            } else if (checkedId == R.id.rb2) {
                sex = "2";
            }
        });

        viewBinding.btnSave.setOnClickListener(v -> {
            if (TextUtils.isEmpty(viewBinding.etUsername.getText().toString().trim())) {
                ToastUtil.showCenter("账号不能为空");
            } else if (TextUtils.isEmpty(viewBinding.etName.getText().toString().trim())) {
                ToastUtil.showCenter("员工姓名不能为空");
            } else if (TextUtils.isEmpty(viewBinding.etPhone.getText().toString().trim())) {
                ToastUtil.showCenter("手机号不能为空");
            } else if (TextUtils.isEmpty(viewBinding.etIdNumber.getText().toString().trim())) {
                ToastUtil.showCenter("身份证号不能为空");
            } else {
                YUtils.showLoading("提交中...");
                Map<String, Object> mapEmployee = new HashMap<>();
                mapEmployee.put("updateUser", employeeBean.getData().getUsername());
                mapEmployee.put("username", viewBinding.etUsername.getText().toString().trim());
                mapEmployee.put("name", viewBinding.etName.getText().toString().trim());
                mapEmployee.put("phone", viewBinding.etPhone.getText().toString().trim());
                mapEmployee.put("sex", sex);
                mapEmployee.put("idNumber", viewBinding.etIdNumber.getText().toString().trim());
                if (id != null && !"".equals(id)) {
                    YUtils.showLoading("提交中...");
                    mapEmployee.put("id", id);
                    presenter.update(mapEmployee);//修改员工信息
                } else {
                    mapEmployee.put("createUser", employeeBean.getData().getUsername());
                    presenter.save(mapEmployee);//添加员工
                }
            }
        });
    }

    @Override
    public void onError(String msg) {
        YUtils.hideLoading();
        ToastUtil.showCenter(msg);
    }

    @Override
    public void saveData(BaseBean bean) {
        YUtils.hideLoading();
        ToastUtil.showCenter(bean.getMsg());
        if (bean.getCode() == 200) {
            setResult(Activity.RESULT_OK);
            finish();
        }
    }

    @Override
    public void updateData(BaseBean bean) {
        YUtils.hideLoading();
        ToastUtil.showCenter(bean.getMsg());
        if (bean.getCode() == 200) {
            Intent intent = new Intent();
            intent.putExtra("name", viewBinding.etName.getText().toString().trim());
            intent.putExtra("username", viewBinding.etUsername.getText().toString().trim());
            intent.putExtra("phone", viewBinding.etPhone.getText().toString().trim());
            intent.putExtra("sex", sex);
            intent.putExtra("idNumber", viewBinding.etIdNumber.getText().toString().trim());
            setResult(Activity.RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public void employeeDetailsData(EmployeeDetailsBean bean) {
        YUtils.hideLoading();
        if (bean.getCode() == 200) {
            viewBinding.etUsername.setText(bean.getData().getUsername());
            viewBinding.etName.setText(bean.getData().getName());
            viewBinding.etPhone.setText(bean.getData().getPhone());
            sex = bean.getData().getSex();
            if ("1".equals(sex)) {
                viewBinding.rb1.setChecked(true);
            } else {
                viewBinding.rb2.setChecked(true);
            }
            viewBinding.etIdNumber.setText(bean.getData().getIdNumber());

            if ("admin".equals(bean.getData().getUsername()) &&
                    !"admin".equals(employeeBean.getData().getUsername())) {
                //当前获取信息为管理员信息，并且不是管理员账号登录的时候禁止修改管理员信息
                viewBinding.etUsername.setEnabled(false);
                viewBinding.etName.setEnabled(false);
                viewBinding.etPhone.setEnabled(false);
                viewBinding.rg.setEnabled(false);
                viewBinding.etIdNumber.setEnabled(false);
                viewBinding.btnSave.setEnabled(false);
            }
        }
    }
}